package com.baizhi.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import cn.hutool.http.HttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@ResponseBody
public class CodeController {

    @RequestMapping("/getCode")
    public void test(HttpSession session, HttpServletResponse response) throws IOException {
        //定义图形验证码的长和宽  生成验证码对象
        ICaptcha lineCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);

        //获取生成的验证码的值
        String code = lineCaptcha.getCode();
        //System.out.println(code);

//        将验证码对象放在session中
        session.setAttribute("code", lineCaptcha);
//        写出到浏览器
        lineCaptcha.write(response.getOutputStream());
    }
}
